/**
 * 02/nov/2011
 */
package edu.android;

import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author fabiomelillo@gmail.com
 * 
 */
public class ContentUse extends Activity {

	MovieArrayAdapter movieAdapter;
	EditText regista;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contentprovideruse);

		Button cerca = (Button) findViewById(R.id.findit);
		final EditText regista = (EditText) findViewById(R.id.regista);

		cerca.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Uri myUri = MovieMetaData.CONTENT_URI;
				String columns[] = new String[] { MovieMetaData.MOVIE_ID,
						MovieMetaData.MOVIE_TITLE_KEY,
						MovieMetaData.MOVIE_DIRECTOR_KEY,
						MovieMetaData.MOVIE_YEAR_KEY,
						MovieMetaData.MOVIE_SEEN_KEY,
						MovieMetaData.MOVIE_HD_KEY,
						MovieMetaData.MOVIE_RATE_KEY };

				String _cerca = regista.getText().toString();
				String[] valori = new String[2];
				valori[0] = "%"+_cerca+"%";
				valori[1] = valori[0];
				String where =MovieMetaData.MOVIE_TITLE_KEY+ " LIKE ? OR " + MovieMetaData.MOVIE_DIRECTOR_KEY + " LIKE ?";

				Cursor c = managedQuery(myUri, columns, where, valori, null);
				final List<Movie> movielist = Movie.createMovieList(c);
				String daStampare;
				if (movielist.size() > 0)
					daStampare = "Primo elemento: " + movielist.get(0);
				else
					daStampare = "Non ci sono film per il filtro impostato";
				
				Toast.makeText(getApplicationContext(),
							daStampare,
							Toast.LENGTH_LONG).show();
			}
		});

		// movieAdapter = new MovieArrayAdapter(this, R.id.layoutListaFilm,
		// movielist);
		// Toast.makeText(getApplicationContext(),
		// "Film in prima posizione: "+movielist.get(0), Toast.LENGTH_LONG);

	}

}
